<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :md="6" :sm="8">
            <a-form-item label="文件分类">
              <a-select placeholder="请选择文件分类" v-model="queryParam.categoryId">
                <a-select-option v-for="(category,categoryIndex) in fileCategoryList" :key="categoryIndex.toString()"
                                 :value="category.id">
                  {{ category.name }}
                </a-select-option>
              </a-select>
            </a-form-item>

          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="文件名">
              <a-input placeholder="请输入文件名" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="文件后缀">
              <a-input placeholder="请输入文件后缀" v-model="queryParam.suffix"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <template slot="fileCategorySlot" slot-scope="text, record, index">
          <div >
            {{getCategory(record.categoryId)}}
          </div>
        </template>
        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <img :src="getAvatarView(record.localUrl)"/>
          </div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDownload(record)">下载</a>

          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <!--<sysFile-modal ref="modalForm" @ok="modalFormOk"></sysFile-modal>-->
  </a-card>
</template>

<script>
  // import SysFileModal from './modules/SysFileModal'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import {doMian} from '@/api/api'
  import {getAction, deleteAction, putAction, postAction, putActionObj} from '@/api/manage'

  export default {
    name: "SysFileList",
    mixins: [JeecgListMixin],
    components: {
      // SysFileModal
    },
    data() {
      return {
        description: '上传文件管理页面',
        fileCategoryList: [],
        // 表头
        columns: [
          {
            title: '文件分类',
            align: "center",
            dataIndex: 'categoryId',
            scopedSlots:{customRender:"fileCategorySlot"}
          },
          {
            title: '文件名',
            align: "center",
            dataIndex: 'name'
          },
          {
            title: '文件后缀',
            align: "center",
            dataIndex: 'suffix'
          },
          {
            title: '预览',
            align: "center",
            dataIndex: 'localUrl',
            scopedSlots:{customRender:"avatarslot"}
          },
          {
            title: '文件大小(Kb)',
            align: "center",
            dataIndex: 'size',
            customRender:function(text){
              return Math.floor(text/1024);
            }
          },
          {
            title: '文件描述',
            align: "center",
            dataIndex: 'description'
          },
          {
            title: '查看次数',
            align: "center",
            dataIndex: 'checkTimes'
          },
          {
            title: '下载次数',
            align: "center",
            dataIndex: 'downloadTimes'
          },
          {
            title: '文件标签',
            align: "center",
            dataIndex: 'tag'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            scopedSlots: {customRender: 'action'},
          }
        ],
        url: {
          list: "/system/sysFile/list",
          delete: "/system/sysFile/delete",
          deleteBatch: "/system/sysFile/deleteBatch",
          exportXlsUrl: "system/sysFile/exportXls",
          importExcelUrl: "system/sysFile/importExcel",
          downloadUrl: "system/sysFile/download",
          queryAllFileCategoryUrl: "system/sysFile/queryAllFileCategory",
          imgerver:doMian+"/sys/common/view"
        },
      }
    },
    created() {
      this.initFileCategory();
    },
    computed: {
      // importExcelUrl: function(){
      //   return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      // }
    },
    methods: {
      initFileCategory: function () {
        getAction(this.url.queryAllFileCategoryUrl, "").then((res) => {
          if (res.success) {
            this.fileCategoryList = res.result;
          } else {
            console.error(res);
          }
        })
      },
      handleDownload: function (record) {
        window.location.href = doMian + this.url.downloadUrl + "/" + record.id;
      },
      getAvatarView:function(avatar){
        return this.url.imgerver +"/"+ avatar;
      },
      getCategory:function(categoryId){
        let categoryName="未分类";
        try{
          this.fileCategoryList.forEach(item => {
            if(item.id==categoryId){
              categoryName=item.name;
              throw new Error("ending");
            }
          });
        }catch(e){
        }
        return categoryName;
        // return this.fileCategoryList[categoryId].name;
      },
    }
  }
</script>
<style lang="less" scoped>
  /** Button按钮间距 */
  .ant-btn {
    margin-left: 3px
  }

  .ant-card-body .table-operator {
    margin-bottom: 18px;
  }

  .ant-table-tbody .ant-table-row td {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .anty-row-operator button {
    margin: 0 5px
  }

  .ant-btn-danger {
    background-color: #ffffff
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }
  .anty-img-wrap{height:25px;position: relative;}
  .anty-img-wrap > img{max-height:100%;}
</style>